// pages/contract/contract.js
const app = getApp();
import Toast from '../../component/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        serviceData: {},
        userName: '',
        isSpread: false,
        authChecked:false,
        flag:true
    },
    priceUnitClick() {
        this.setData({
            isSpread: !this.data.isSpread
        });
    },
    payClick() {
        // if (app.userInfo.IsVerified == 0 || app.userInfo.IsVerified == 1) {
        //     Toast("您还未实名认证，请先实名认证！");
        //     return;
        // }
        if(this.data.authChecked){
            let parmas = this.data.serviceData;
            console.log(parmas)
            let data = {
                ServiceId: parmas.Id,
                UserInfoId: app.userInfo.Id
            };
            console.log("url:" + app.globalData.serverAddress.payService + "data：" + JSON.stringify(data));
            wx.request({
                url: app.globalData.serverAddress.payService,
                data: data,
                header: {
                    'Authorization': "Bearer " + app.globalData.token
                },
                method: 'POST',
                success: function (res) {
                    console.log(res.data);
                    if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                        console.log(res.data)
                        wx.requestPayment({
                            timeStamp: res.data.BodyContent.timeStamp,
                            nonceStr: res.data.BodyContent.nonceStr,
                            package: res.data.BodyContent.package,
                            signType: res.data.BodyContent.signType,
                            paySign: res.data.BodyContent.paySign,
                            success: function (res) {
                                wx.navigateBack({
                                    delta: 5,
                                });
                            },
                            fail: function (res) {
                                console.log("fail" + JSON.stringify(res));
                            },
                            complete: function (res) { },
                        })
                    }
                    Toast(res.data.Message);
                },
                fail: function (res) {
                    Toast("订单提交失败，请检查网络");
                    console.log("fail：" + JSON.stringify(res))
                },
                complete: function (res) { },
            })
        }else{
            Toast("请阅读协议后再提交");
        }
    },
    checkboxChange(e) {
        let currentCheck = this.data.authChecked;
        this.setData({
            authChecked: !currentCheck
        });
    },
    toAgreement() {
        app.globalData.globalWebUrl = app.globalData.contractUrl;
        wx.navigateTo({
            title:'闲将服务基础合同',
            url: '/pages/agreement-web/agreement-web?title=闲将服务基础合同',
        })
    },
    cancel(){
        wx.navigateBack({
            delta: 1,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options);
        let service = app.globalData.serviceInfo;
        let flag = options.flag == 'true';
        // console.log(flag);
        this.setData({
            serviceData: service,
            flag: flag,
            authChecked:!flag
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})